# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=poppler
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-qt5" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"))
pkgver=25.05.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://poppler.freedesktop.org/"
msys2_repository_url="https://gitlab.freedesktop.org/poppler/poppler"
msys2_references=(
  "cpe: cpe:/a:freedesktop:poppler"
  "cpe: cpe:/a:poppler:poppler"
)
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-base" \
               "${MINGW_PACKAGE_PREFIX}-qt6-base")
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler-data"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://poppler.freedesktop.org/${_realname}-${pkgver}.tar.xz{,.sig}
        test::git+https://gitlab.freedesktop.org/poppler/test.git#commit=ff3133cdb6cb496ee1d2c3231bfa35006a5e8410
        '0001-nopython-generate-enums.patch')
sha256sums=('9b1627c5b76816ac5e4052a03f5b605ba40b45cf06b02cadd0479620b499ab38'
            'SKIP'
            '0efc9bd1797f0f0dfa514d4109e82c99d7e98c3e95587c70945508493074fcdf'
            '315356263b5ebee5431a2e079ac76492a8919a04a798f6c6d53c64f44b433db6')
validpgpkeys=('CA262C6C83DE4D2FB28A332A3A6A4DB839EAA6D7') # "Albert Astals Cid <aacid@kde.org>"

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}/0001-nopython-generate-enums.patch"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != i686 ]]; then
    _extra_config+=("-DENABLE_QT5=ON" "-DENABLE_QT6=ON")
  else
    _extra_config+=("-DENABLE_QT5=OFF" "-DENABLE_QT6=OFF")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
    -DENABLE_GLIB=ON \
    -DENABLE_GOBJECT_INTROSPECTION=ON \
    -DENABLE_CPP=ON \
    -DENABLE_DCTDECODER=libjpeg \
    -DENABLE_LIBOPENJPEG=openjpeg2 \
    -DENABLE_NSS3=ON \
    -DENABLE_GPGME=OFF \
    -DENABLE_UTILS=ON \
    -DENABLE_LIBCURL=ON \
    -DENABLE_ZLIB_UNCOMPRESS=OFF \
    -DENABLE_GTK_DOC=OFF \
    -DENABLE_RELOCATABLE=ON \
    -DBUILD_CPP_TESTS=OFF \
    -DBUILD_GTK_TESTS=OFF \
    -DBUILD_MANUAL_TESTS=OFF \
    -DBUILD_QT5_TESTS=OFF \
    -DBUILD_QT6_TESTS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -DBUILD_CPP_TESTS=ON \
    -DBUILD_GTK_TESTS=ON \
    -DBUILD_MANUAL_TESTS=ON \
    -DBUILD_QT5_TESTS=ON \
    -DBUILD_QT6_TESTS=ON \
    ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake.exe --build .

  # Prepend build directory with PATH to get DLL file
  PATH=$PWD:$PWD/cpp:$PWD/qt5/src:$PWD/qt6/src:$PATH \
    ${MINGW_PREFIX}/bin/ctest.exe . || warning "Tests failed"
}

package_poppler() {
  pkgdesc='PDF rendering library based on xpdf 3.0 (mingw-w64)'
  optdepends=("${MINGW_PACKAGE_PREFIX}-glib2: libpoppler-glib")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # cleanup for splitted build
  rm -vrf "${pkgdir}${MINGW_PREFIX}"/include/poppler/{qt5,qt6}
  rm -vf "${pkgdir}${MINGW_PREFIX}"/bin/libpoppler-{qt5,qt6}*
  rm -vf "${pkgdir}${MINGW_PREFIX}"/lib/libpoppler-{qt5,qt6}*
  rm -vf "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/poppler-{qt5,qt6}.pc
}

package_poppler-qt5() {
  pkgdesc='Poppler Qt5 bindings (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-poppler=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt5-base")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target qt5/install
  install -Dm644 poppler-qt5.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/poppler-qt5.pc"
}

package_poppler-qt6() {
  pkgdesc='Poppler Qt6 bindings (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-poppler=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt6-base")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target qt6/install
  install -Dm644 poppler-qt6.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/poppler-qt6.pc"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
